<template>
  <div class="home-container">
    <!-- 顶部区域 -->
    <div class="top-area" v-if="!shouldHideTopArea">
      <!-- 搜索框 -->
      <div class="search-container">
        <van-search
            v-model="searchValue"
            placeholder="搜索商品、品牌、分类"
            shape="round"
            @focus="goToSearch"
            background="linear-gradient(135deg, #dfe684 0%, #c5eda5 100%)"
        />
      </div>

      <!-- 通知图标 -->
      <div class="notification-icon" @click="goToNotifications">
        <van-icon name="chat-o" size="24" />
        <span class="notification-badge" v-if="notificationStore.unreadCount > 0">
          {{ notificationStore.unreadCount > 99 ? '99+' : notificationStore.unreadCount }}
        </span>
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="main-content">
      <router-view/>
    </div>
    <div v-if="!shouldHideTabbar">
      <!-- 底部导航栏 -->
      <tabbar/>
    </div>
  </div>

</template>

<script setup lang="ts">
import { ref} from 'vue';
import { useRoute } from 'vue-router';
import { useRouter } from 'vue-router';
import tabbar from '@/components/tab-bar.vue';
import { computed } from 'vue';
// 在 script setup 部分添加
import { useNotificationStore } from '@/store/notification';
import {onMounted} from "vue";

const router = useRouter();
const route = useRoute();
const searchValue = ref('');

//定义需要隐藏顶部搜索框的页面路径
const hideTopArea=['/notifications', '/messageDetail/:id', '/orderDetail/:id/:status', '/commentDetail/:id/:status', '/square']
// 定义需要隐藏底部导航栏的页面路径
const hideTabbarRoutes = [
  '/notifications',
    '/messageDetail/:id',
    '/orderDetail/:id/:status',
    '/commentDetail/:id/:status',
]
// 判断当前路由是否需要隐藏底部导航栏
const shouldHideTabbar = computed(() => {
  return hideTabbarRoutes.some(routePattern => {
    // 将路由模式转换为正则表达式
    const pattern = routePattern.replace(/\/:([^/]+)/g, '/[^/]+')
    const regex = new RegExp(`^${pattern}$`)
    return regex.test(route.path)
  })
})
//判断是否需要隐藏顶部搜索框
const shouldHideTopArea = computed(() => {
  // 特殊处理所有 Square 组件路由
  const squareRoutes = [
    '/square',
    '/clock-in',
    '/comment',
    '/diet',
    '/postfs',
    '/qa',
    '/tutorial',
    '/experience',
    '/follow'
  ];

  if (squareRoutes.includes(route.path)) {
    return true;
  }
  return hideTopArea.some(routePattern => {
    // 将路由模式转换为正则表达式
    const pattern = routePattern.replace(/\/:([^/]+)/g, '/[^/]+')
    const regex = new RegExp(`^${pattern}$`)
    return regex.test(route.path)
  })
})
//搜索
const goToSearch = () => {
  router.push('/search');
};

//通知
const notificationStore = useNotificationStore();
onMounted(async () => {
  // 从API获取通知数据，store内部会处理API失败或无数据的情况
  await notificationStore.fetchNotificationsFromApi();
});
const goToNotifications = () => {
  router.push('/notifications');
};

</script>

<style scoped>
.home-container {

}

.top-area {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: linear-gradient(135deg, #dfe684 0%, #c5eda5 100%);
  box-shadow: 0 2px 12px rgba(100, 101, 102, 0.12);
  gap: 12px;
}

.search-container {
  flex: 1;
  max-width: calc(100% - 50px);
  background: linear-gradient(135deg, #dfe684 0%, #c5eda5 100%);
}

.notification-icon {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.15);
  transition: all 0.3s ease;
  flex-shrink: 0;
  cursor: pointer;
}

.notification-icon:active {
  background: rgb(7, 96, 161);
  transform: scale(0.95);
}

.notification-badge {
  position: absolute;
  top: 2px;
  right: 2px;
  background: linear-gradient(135deg, #ff6b6b 0%, #ee0a24 100%);
  color: white;
  font-size: 12px;
  min-width: 15px;
  height: 10px;
  border-radius: 9px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 4px;
  border: 2px solid #6a11cb;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

/* 响应式设计 */
@media (max-width: 360px) {
  .top-area {
    padding: 10px 12px;
  }

  .notification-icon {
    width: 40px;
    height: 40px;
  }
}

/* 滑动过渡效果 */
.slide-enter-active,
.slide-leave-active {
  transition: transform 0.3s ease, opacity 0.3s ease;
}

.slide-enter-from {
  transform: translateX(100%);
  opacity: 0;
}

.slide-leave-to {
  transform: translateX(-100%);
  opacity: 0;
}

/* 淡入淡出效果 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>